<template>
  <div>
    <el-main>
      <el-button type="primary" plain icon="el-icon-plus" @click="add()"
        >添加分类</el-button
      >
      <el-table :data="tableData">
        <el-table-column fixed prop="id" label="ID" width="140">
        </el-table-column>
        <el-table-column prop="name" label="分类" width="120">
        </el-table-column>
        <el-table-column prop="disabled" label="状态"
          ><template slot-scope="scope">
            <span v-if="scope.row.disabled === true" style="color: #f56c6c"
              >禁用</span
            >
            <span v-if="scope.row.disabled === false" style="color: #67c23a"
              >启用</span
            >
          </template>
        </el-table-column>
        <el-table-column fixed="right" label="操作">
          <template slot-scope="scope">
            <span>
              <el-button
                type="danger"
                icon="el-icon-delete"
                plain
                @click="completeDel(scope.row)"
                size="small"
                >删除</el-button
              >
            </span>
          </template>
        </el-table-column>
      </el-table>
    </el-main>
    <!-- pagination -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="current"
      :page-sizes="[5, 10, 20]"
      :page-size="size"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    >
    </el-pagination>

    <!-- addForm -->
    <el-dialog title="新增分类" :visible.sync="addFormVisible">
      <el-form :model="ruleForm">
        <el-form-item label="名称" :label-width="formLabelWidth">
          <el-input v-model="ruleForm.name" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="addFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleAdd()">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { getInfo, add, del } from "@/api/category";

export default {
  name: "Category",
  data() {
    return {
      loadding: false,
      addFormVisible: false,
      editFormVisible: false,
      formLabelWidth: "120px",
      ruleForm: {
        id: "",
        name: "",
      },
      tableData: null,
      current: 1,
      size: 5,
      total: null,
    };
  },
  mounted() {
    this.handleCurrentChange(1);
  },
  methods: {
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      getInfo(this.current, val)
        .then((res) => {
          console.log(res);
          this.tableData = res.records;
          this.total = res.total;
          this.current = res.current;
          this.size = res.size;
        })
        .catch((error) => {
          console.log(error);
        });
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      getInfo(val, this.size).then((res) => {
        console.log(res);
        this.tableData = res.records;
        this.total = res.total;
        this.current = res.current;
        this.size = res.size;
      });
    },
    add() {
      this.addFormVisible = true;
      this.ruleForm.name = null;
    },
    handleAdd() {
      this.addFormVisible = false;
      console.log("add");
      add(this.ruleForm.name).then((res) => {
        console.log(res);
        this.$message("添加成功");
        this.addFormVisible = false;
        this.handleCurrentChange(this.current);
      });
    },
    // 完全删除
    completeDel(val) {
      this.ruleForm = val;
      del(this.ruleForm).then((res) => {
        console.log(res);
        this.$message(res);
        this.handleCurrentChange(this.current);
      });
    },
  },
};
</script>